/**
 * 
 */
package optimization.naturalOptimization.fitness;

import multithreading.FitnessCalculator;
import optimization.naturalOptimization.population.Population;

/**
 * @author Kevin Wagner
 * @version 1.0
 */
public class Rastrigin extends SimpleFitnessFunction<double[]> {

	/* (non-Javadoc)
	 * @see optimization.IdentificationI#getIndentification()
	 */
	@Override
	public String getIndentification() {
		return "Test Function: Rastrigin's function";
	}

	/* (non-Javadoc)
	 * @see optimization.naturalOptimization.fitness.SimpleFitnessFunction#getFitness(java.lang.Object)
	 */
	@Override
	public double getFitness(double[] position) {
		double erg=10.0*position.length;
		for(int i=0;i<position.length;i++){
			erg+=position[i]*position[i]-10*Math.cos(2*Math.PI*position[i]);
		}
		return erg;
	}

	/* (non-Javadoc)
	 * @see optimization.naturalOptimization.fitness.SimpleFitnessFunction#getFitness(optimization.naturalOptimization.population.Population)
	 */
	@Override
	public void getFitness(Population pop) throws Exception {
		int ind=pop.getNumberOfIndividuals();
		FitnessCalculator[] calculators=new FitnessCalculator[ind];
		for(int i=0;i<ind;i++){
			calculators[i]=new FitnessCalculator(pop.getIndividual(i), this);
			calculators[i].start();
		}
		for(int i=0;i<ind;i++){
				calculators[i].join();
		}
	}

}
